/*  PrimeBlaster
    by Kjetil Eide 
    kjetile445@gmail.com

    This file is part of PrimeBlaster

    PrimeBlaster is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PrimeBlaster is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PrimeBlaster.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.eide.spaceship;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class LevelGetReady extends BasicGameState {

	/* the time in milliseconds to display the "get ready"
	 * message for level 1 and level > 1
	 */
	private static final int MSG_DELAY1 = 4000;
	private static final int MSG_DELAY2 = 1000;
	
	/** The ID given to this state */
	public static final int ID = 2;
	
	private int msg_delay;
	private long start;
	private PlayerStatus playerStatus;
	
	public LevelGetReady(PlayerStatus playerStatus) {
		this.playerStatus = playerStatus;
	}
	
	@Override
	public int getID() {
		return ID;
	}

	@Override
	public void init(GameContainer container, StateBasedGame game)
			throws SlickException {
	}

	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		if (playerStatus.getLevel() <= PlayerStatus.MAX_LEVEL) {
			g.drawString("Get ready for level " + playerStatus.getLevel(), 100, 100);
			if (playerStatus.getLevel() == 1) {
				g.drawString("Shoot dividable asteroids", 100, 130);
			}
		} else {
			g.drawString("You completed all levels", 100, 100);
			g.drawString("Coungratulations!", 100, 130);
		}
	}

	@Override
	public void update(GameContainer container, StateBasedGame game, int delta)
			throws SlickException {
		if ((System.currentTimeMillis() - start) > msg_delay) {
			//game.enterState(MainGame.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
			if (playerStatus.getLevel() <= PlayerStatus.MAX_LEVEL) {
				game.enterState(MainGame.ID);
			} else {
				game.enterState(TitleScreen.ID);
			}
		}	
	}
	
	public void enter(GameContainer container, StateBasedGame game) throws SlickException {
		super.enter(container, game);
		playerStatus.increaseLevel();
		start = System.currentTimeMillis();
		if (playerStatus.getLevel() == 1 || playerStatus.getLevel() > PlayerStatus.MAX_LEVEL) {
			msg_delay = MSG_DELAY1;
		} else {
			msg_delay = MSG_DELAY2;
		}
	}
}
